import { defineStore, storeToRefs } from "pinia";
import { updateNotice, getNewNoticeApi, getAllNoticeApi } from "@/service/update-notice";
import { useCommonStore } from "../commonStore";
import { showElMessage } from '@/utils/showTipMessage'
const commonStore = useCommonStore();
const { isShowCover } = storeToRefs(commonStore);
export const useUpdateNotice = defineStore('updateNotice', {
    state() {
        return {
            noticeInfo: null,
            allNotices: [],
            keys: []
        }
    },
    actions: {
        // 发布公告
        async updateNoticeRequest(form) {
            const name = localStorage.getItem("realName");
            const nameId = localStorage.getItem("id");
            const res = await updateNotice(name, nameId, form);
            const code = res.data.code;
            if (code === 0) {   // 表示公告内容添加成功
                // 1、退出编辑面板
                isShowCover.value = false;
                // 2、刷新当前页面
                showElMessage('公告发布成功！');
            }
        },
        async getNewNotice() {
            const res = await getNewNoticeApi();
            this.noticeInfo = res.data?.data;
        },
        async getAllNotice() {
            const res = await getAllNoticeApi();
            this.allNotices = res.data?.data;
            this.keys = Object.keys(res.data.data[0])
        }
    }
})